<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled"  style="margin-bottom: 50px;">
      <a-form :form="form" slot="detail">
        <a-row  v-for="(item,index) in advList" :key="index">
          <a-col :span="24">
            <a-form-item label="广告图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <span style="color: #ff0000">(图片尺寸:16:9 图片大小：＜1M)</span>
              <j-image-upload bizPath="commodity/adv"   isMultiple :number="1"  v-model="item.advImg"></j-image-upload>
              <div class="ant-form-explain" style="color: #ff0000;" v-if="!item.advImg">{{messageList[index].advImg}}</div>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="跳转链接" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="item.toUrl" :maxLength="255"  placeholder="请输入跳转链接"   :style="index>0?'float: left;width: 75%':''" ></a-input>
              <a-button class="new-btn" v-if="index>0" type="dashed" @click="deletePro(index)" style="margin-left: 10px;vertical-align: super;color:#ff0000;">
                <a-icon type="minus"/>
                删除记录
              </a-button>
              <div class="ant-form-explain" style="color: #ff0000;" v-if="!item.toUrl">{{messageList[index].toUrl}}</div>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row v-if="advList.length<8">
          <a-col :span="24"  align="center" >
            <a-button class="new-btn" type="dashed" @click="addNewPro">
              <a-icon type="plus"/>
              添加新记录
            </a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'QyCommodityAdvForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/qy/qyCommodityAdv/add",
          edit: "/qy/qyCommodityAdv/edit",
          queryById: "/qy/qyCommodityAdv/queryById",
          list:"/qy/qyCommodityAdv/list"
        },
        advList:[],
        messageList:[],
        typeMap:{
          "advImg":"广告图片",
          "toUrl":"跳转链接"
        },
        commodityId:''
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
       // this.edit({});
      },
      edit (record) {
       /* this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'advImg','toUrl'))
        })*/
      },
      //渲染流程表单数据
      showFlowData(){
        /*if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }*/
      },
      submitForm () {
        const that = this;
        if(this.advList){
          var isUp = true;
          for(var k in that.advList){
            var pro = that.advList[k];
            isUp= that.checkInput('advImg',k);
            if(!isUp){
              break;
            }
            isUp= that.checkInput('toUrl',k);
            pro.commodityId = that.commodityId;
          }
          if(isUp){//提交
            that.confirmLoading = true;
            that.$emit("changeLoading",true);
            httpAction(that.url.edit,that.advList,'put').then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit("changeNext",this.commodityId);
              }else{
                that.$message.warning(res.message);
              }
              that.$emit("changeLoading",false);
            }).finally(() => {
              that.confirmLoading = false;
            });
          }
        }
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'advImg','toUrl'))
      },
    getDetail(commodityId){
      let params = {commodityId:commodityId};
      getAction(this.url.list,params).then((res)=>{
        if(res.success){
          if(res.result.length>0){
            this.advList= res.result;
            for(var jk in this.advList){
              this.messageList.push({
                "advImg":"",
                "toUrl":""
              });
            }
          }else{
            console.log(res.result);
            this.advList= [{
              "advImg":"",
              "toUrl":""
            }];
            this.messageList = [{
              "advImg":"",
              "toUrl":""
            }];
          }

        }
      });
      this.commodityId = commodityId;
    },addNewPro(){
      this.advList.push({
        "advImg":"",
        "toUrl":""
      });
      this.messageList.push({
        "advImg":"",
        "toUrl":""
      });
    },deletePro(idx){
      this.advList.splice(idx,1);
      this.messageList.splice(idx,1);
    },checkInput(cold,idx){
      if(this.advList[idx][cold]){
        this.messageList[idx][cold] = "";
        return true;
      }else{
        this.messageList[idx][cold] = this.typeMap[cold]+"不能为空";
        return false
      }

    }
    }
  }
</script>